package com.husd.framework.a_autocode_new.scanner;

/**
 * 分词器的辅助工具
 *
 * @author shengdonghu@126.com
 * @date 2024/1/22 17:42
 */
public class CharScanner {

    // private String str;

    private int index = 0; // 当前索引
    private char[] charArray;
    private int len = 0;

    public CharScanner(String str) {
        // this.str = str;
        this.charArray = str.toCharArray();
        this.len = str.length();
    }

    private boolean validIndex(int index) {

        return index >= 0 && index <= (len - 1);
    }

    public String strFromNow(int count) {

        int to = this.index + count;
        if (!validIndex(to)) {
            throw new RuntimeException("无效的count 超过了边界: " + count);
        }
        return String.valueOf(this.charArray, this.index, count);
    }

    public char current() {

        if (!validIndex(this.index)) {
            throw new RuntimeException("无效的index:" + index);
        }
        return charArray[index];
    }

    public boolean haveC() {

        return index <= this.len - 1;
    }

    public boolean haveNext() {

        return index <= this.len - 2;
    }

    public boolean next() {
        boolean c = haveNext();
        this.index++;
        return c;
    }
}
